! function() {
    var view = document.querySelector('#NavBar')
    var controller = {
        view: null,
        aTags: null,
        init: function(view) {
            this.view = view
            this.aTags = view.querySelectorAll('nav >li >a')
            this.initAnimation()
            this.bindEvents()
        },
        initAnimation: function() {
            function animate(time) {
                requestAnimationFrame(animate)
                TWEEN.update(time)
            }
            requestAnimationFrame(animate)
        },
        bindEvents: function() {
            var aTags = this.aTags
            for (let i = 0; i < aTags.length; i++) {
                aTags[i].onclick = (e) => {
                    e.preventDefault()
                    let element = document.querySelector(e.currentTarget.getAttribute('href'))
                    this.scrollToElement(element)
                }
            }
        },
        scrollToElement: function(element) {
            let top = element.offsetTop
            let currentTop = window.scrollY
            let targetTop = top - 80
            let s = Math.abs(targetTop - currentTop) //路程
            var coords = { y: currentTop }; //起始位置
            var t = s
            if (t > 500) { t = 500 }
            var tween = new TWEEN.Tween(coords)
                .to({ y: targetTop }, t) //时间
                .easing(TWEEN.Easing.Quadratic.InOut) //效果
                .onUpdate(function() { //动作
                    window.scrollTo(0, coords.y)
                })
                .start();
        },
    }
    controller.init(view)
}.call()